import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MatDialog } from '@angular/material';
import {tap} from 'rxjs/operators';
import { BackaddComponent } from '../backadd/backadd';
import { Asset } from '../../models/Asset';
import { DeliverService } from '../../service/deliverService';
import { Loginauth } from '../../service/loginauth';
import { AssetService } from '../../service/assetService';
import { User } from '../../models/User';
import { Department } from '../../models/Department';
import { environment } from '../../environments/environment';
import { Moment } from 'moment';
import { FormControl } from '@angular/forms';
import {ConfirmDialogComponent} from '../confirmDialog/confirmDialog';
import {AssetaddComponent} from '../assetadd/assetadd';
declare var $: any;
@Component({
  templateUrl: './assetenterInfo.html',
  styleUrls: ['./assetenterInfo.css']
})

export class AssetenterInfoComponent implements OnInit {
  assets: Asset[];
  total: number;
  page: number = 1;
  pageSize: number = 5;
  asset: Asset;
  isAdmin: boolean;
  environment = environment;
  start_createDate: any;
  end_createDate: any;
  beginCtrl = new FormControl();
  endCtrl = new FormControl();
  baseurl: string;
  imgAppear: boolean;
  codeAppear: boolean;
  imgUrl1: string;
  imgUrl2: string;
  imgUrl3: string;
  haveimg3: boolean = false;
  haveimg2: boolean = false;
  ids: string[] = [];
  colsone = [ 'rukuNum', 'assetName', 'assetNum', 'createDate'];
  colstwo = [ 'assetType', 'brand', 'source', 'version', 'sourceComefrom',
    'assetPrice', 'buyDate', 'snNum', 'assetImg', 'user', 'department', 'address', 'seatNum', 'invoiceNum', 'qrcode', 'remark' ];
  colsthree = ['operate'];
  constructor(private route: ActivatedRoute, private dialog: MatDialog,
              private deliverService: DeliverService,
              private loginauthService: Loginauth, private assetService: AssetService) {

  }
  ngOnInit() {
    this.loginauthService.checkAdmin().subscribe((isAdmin: boolean) => {
      this.isAdmin = isAdmin;
    })
    this.baseurl = 'http://file.sxycy.cn:8081';
    this.asset = new Asset();
    this.asset.sysUser = new User();
    this.asset.sysDepartment = new Department();
    this.asset.assetinfos = [];
    this.update(this.page).subscribe((res) => {
      window.setInterval(() => {
        this.setHeight();
      }, 100);
    });

  }
  setHeight() {
    for ( let i = 0; i < this.assets.length; i++) {
      const asset = this.assets[i];
      $('#' + asset.rukuNum).height(asset.assetinfos.length * 48 - 1.4 + 'px');
      $('#' + asset.rukuNum + '0').height(asset.assetinfos.length * 48 - 1.4 + 'px');
    }
  }

  bigImg(imgUrl1, imgUrl2, imgUrl3) {
    this.imgUrl1 = this.baseurl + imgUrl1;
    if (imgUrl2 !== null && imgUrl2 !== 'null' && imgUrl2) {
      this.haveimg2 = true;
      this.imgUrl2 = this.baseurl + imgUrl2;
    } else {
      this.haveimg2 = false;
    }
    if (imgUrl3 !== null && imgUrl3 !== 'null' && imgUrl3) {
      this.haveimg3 = true;
      this.imgUrl3 = this.baseurl + imgUrl3;
    } else {
      this.haveimg3 = false;
    }
    this.imgAppear = true;
  }
  bigCode(imgUrl1) {
    console.log(imgUrl1);
    this.imgUrl1 = this.baseurl + imgUrl1;
    this.codeAppear = true;
  }
  closeImg() {
    this.imgAppear = false;
    this.imgUrl1 = '';
    this.imgUrl2 = '';
  }
  closeCode() {
    this.codeAppear = false;
  }
  onopenAdd() {
    const dialogRef = this.dialog.open(BackaddComponent, {
      width: '900px',
      height: '500px',
    });
    dialogRef.afterClosed().subscribe((result) => {
      this.update(this.page).subscribe();
    })
  }
  onSwitchDate(_: Moment) {
    this.start_createDate = this.beginCtrl.value;
    this.end_createDate = this.endCtrl.value;
    this.update(this.page).subscribe();
  }
  update(page) {
    const options: any = {};
    if (this.asset.assetName) {options.assetName = this.asset.assetName; }
    if (String(this.asset.assetStatus) && String(this.asset.assetStatus) !== 'null') {options.assetStatus = this.asset.assetStatus; }
    if (this.asset.repairStatus) {options.repairStatus = this.asset.repairStatus; }
    if (this.asset.assetCode) {options.assetCode = this.asset.assetCode; }
    if (this.asset.assetVersion) {options.assetVersion = this.asset.assetVersion; }
    if (this.asset.sourceComefrom) {options.sourceComefrom = this.asset.sourceComefrom; }
    if (this.asset.assetBrand) {options.assetBrand = this.asset.assetBrand; }
    if (this.asset.sysUser.username) {options.assetName = this.asset.sysUser.username; }
    if (this.asset.address) {options.address = this.asset.address; }
    if (this.asset.seatNum) {options.seatNum = this.asset.seatNum; }
    if (this.asset.snNum) {options.snNum = this.asset.snNum; }
    if (this.asset.invoiceNum) {options.invoiceNum = this.asset.invoiceNum; }
    if (this.asset.remark) {options.remark = this.asset.remark; }
    if (this.asset.assetType) {options.assetType = this.asset.assetType; }
    if (this.asset.source) {options.source = this.asset.source; }
    if (this.asset.sysDepartment.id) {options.assetDepartmentId = this.asset.sysDepartment.id; }
    if (this.asset.rukuNum) {options.rukuNum = this.asset.rukuNum; }
    if (this.asset.sysUser.id) {options.assetUserId = this.asset.sysUser.id; }
    if (this.start_createDate) {const start = this.start_createDate.toDate(); options.start_createDate = start; }
    if (this.end_createDate) {const end = this.end_createDate.toDate(); options.end_createDate = end ; }
    return this.assetService.getAssetsinfo(page, this.pageSize, options).pipe(tap((res) => {
      this.assets = res.res;
      this.total = res.total;
      this.page = page;
    }));

  }
  trackById(index: number, item: any) {
    return item.id;
  }
  onPageChanged(page: number) {
    this.page = page;
    this.update(page).subscribe();
  }
  search() {
    this.update(this.page).subscribe();
  }
  onDeleteruku(rukuNum) {
    const dialogRef = this.dialog.open(ConfirmDialogComponent, {
      width: '400px',
      height: '200px',
      data: {message: '确认删除？'}
    });
    dialogRef.afterClosed().subscribe((result) => {
      if (result) {
        this.assetService.delbatch(rukuNum).subscribe((res) => {
          if (res.success) {
            this.update(this.page).subscribe();
          }
        })
      }
    });
  }
  onEditruku(asset) {
    const dialogRef = this.dialog.open(AssetaddComponent, {
      width: '900px',
      height: '550px',
      data: {
        asset: asset.assetinfos[0],
        assets: asset
      }
    });
    dialogRef.afterClosed().subscribe((result) => {
      this.update(this.page).subscribe();
    });
  }
}
